//
//  UserAccount.swift
//  WeiBo001
//
//  Created by Zhanghong on 15/6/28.
//  Copyright © 2015年 HeiMa. All rights reserved.
//

import UIKit

class UserAccount: NSObject,NSCoding {
    //用于调用access_token，接口获取授权后的access token。
    var access_token : String
    //access_token的生命周期，单位是秒数。
    var expires_in : NSTimeInterval
    // 当前授权用户的UID。
    var uid : String
    
    var expiresDate : NSDate
    
    var name : String?
    var avatar_large : String?
    
    init(dict: [String: AnyObject]) {
        access_token = dict["access_token"] as! String
        expires_in = dict["expires_in"] as! NSTimeInterval
        uid = dict["uid"] as! String
        
        expiresDate = NSDate(timeIntervalSinceNow: expires_in)
    }
    
    override var description : String {
        let properties = ["access_token","expires_in","uid","avatar_large","name"];
        return "\(self.dictionaryWithValuesForKeys(properties))"
        
    }
    
    func loadUserInfo(finished: (account : UserAccount?, error:NSError?)->()){
        let params = ["access_token":access_token,"uid":uid]
        NetworkTools.shareNetworkTools().GET("2/users/show.json", parameters: params, success: { (_, JSON) -> Void in
            
                let dict = JSON as! [String : AnyObject]
                self.name = dict["name"] as? String
                self.avatar_large = dict["avatar_large"] as? String
                // 保存完整的用户信息
                self.saveAccount()
            
                finished(account: self, error: nil)
            
            }) { (_, error) -> Void in
                finished(account: nil, error: error)
        }
    }
    
    
    static let accountPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last!.stringByAppendingPathComponent("account.plist")
    
    func saveAccount(){
        print(UserAccount.accountPath)
        NSKeyedArchiver.archiveRootObject(self, toFile: UserAccount.accountPath)
    }
    
    class func loadAccount()->UserAccount? {
        print("加载用户")
        if let account = NSKeyedUnarchiver.unarchiveObjectWithFile(accountPath) as? UserAccount {
            if account.expiresDate.compare(NSDate()) == NSComparisonResult.OrderedDescending{
                return account
            }
        }
        
        return nil
    }
    
    
    // 解档方法
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as! String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        uid = aDecoder.decodeObjectForKey("uid") as!String
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as! NSDate
        name = aDecoder.decodeObjectForKey("name") as! String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as! String
    }
    
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(name, forKey: "name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
    
}























